###############################################
#                                             #
#     State Survey Treatment Effects          #
#     Created by Mac Lockhart Aug 07 2023     #
#     Updated Aug 07 2023                     #
#     Creates plots for paper                 #
###############################################

library(tidyverse)
library(data.table)
library(modelsummary)
library(dotwhisker)
library(ggpubr)

#Read in most recent version of cleaned data using:
#Clean_National_Sample.R
if(Sys.info()[7] == "maclockhart"){
  setwd("/Users/maclockhart/Dropbox/School/RA Work/'22 Seth & Thad/MIT Evolving Election Administration Landscape")
}


for(state in c("Colorado", "Georgia", "LA", "Texas")){
  DF<- fread(paste0("Survey Data/", state, " Recoded.csv"))
  DF<-DF[bot==0]
  #Code outcome variables as change from pre-treatment values
  
  #relevel treatment variable in Colorado to compare treatments to the control
  if(state=="Colorado"){DF$state_treatment <- factor(DF$state_treatment, levels=c("Control", "Colorado - Absentee Voting", "Colorado - Facts"))}
  x<-DF |> group_by(d_state_treatment, ownstatepost) |>
    ## add percentage label with `scales::percent()`
    dplyr::mutate(perc = scales::percent(n / sum(n), accuracy = .1, trim = FALSE))
#create barcharts for each outcome with treatment and control next to eachother and differences noted

  p1<-ggplot(DF, aes(x = ownstatepost, fill = factor(d_state_treatment))) +
    geom_bar(position = position_dodge(width = 0.8), width = 0.8, aes(y = ..prop..)) +
    #  geom_text(aes(label="text"), y=0.3, position_dodge(width=0.8)) +
    labs(title = "Trust in own state",
         x = "Trust level reported after viewing video",
         y = "Proportion within Condition",
         fill="Condition") +
    scale_fill_manual(values = c("1" = "black", "0" = "grey"), labels = c("1" = "Treated", "0" = "Control")) +
    theme_minimal() +   scale_x_continuous(breaks = c(1, 2, 3, 4), labels = c("Distrust A Lot", "Distrust Some", "Trust Some", "Truse A Lot"))  # Relabel x ticks
  
  p2<-ggplot(DF, aes(x = otherstatepost, fill = factor(d_state_treatment))) +
    geom_bar(position = position_dodge(width = 0.8), width = 0.8, aes(y = ..prop..)) +
    labs(title = "Trust in other states",
         x = "Trust level reported after viewing video",
         y = "Proportion within Condition",
         fill="Condition") +
    scale_fill_manual(values = c("1" = "black", "0" = "grey"), labels = c("1" = "Treated", "0" = "Control")) +
    theme_minimal() +   scale_x_continuous(breaks = c(1, 2, 3, 4), labels = c("Distrust A Lot", "Distrust Some", "Trust Some", "Truse A Lot"))  # Relabel x ticks
  
  p3<-ggplot(DF, aes(x = votefraudpost, fill = factor(d_state_treatment))) +
    geom_bar(position = position_dodge(width = 0.8), width = 0.8, aes(y = ..prop..)) +
    labs(title = "Belief in fraud by voters",
         x = "Trust level reported after viewing video",
         y = "Proportion within Condition",
         fill="Condition") +
    scale_fill_manual(values = c("1" = "black", "0" = "grey"), labels = c("1" = "Treated", "0" = "Control")) +
    theme_minimal() +   scale_x_continuous(breaks = c(1, 2, 3, 4, 5), labels = c("Almost never", "Infrequently", "Half of the time", "Very common", "All of the time"))  # Relabel x ticks
  
  p4<-ggplot(DF, aes(x = officialfraudpost, fill = factor(d_state_treatment))) +
    geom_bar(position = position_dodge(width = 0.8), width = 0.8, aes(y = ..prop..)) +
    labs(title = "Belief in fraud by officials",
         x = "Trust level reported after viewing video",
         y = "Proportion within Condition",
         fill="Condition") +
    scale_fill_manual(values = c("1" = "black", "0" = "grey"), labels = c("1" = "Treated", "0" = "Control")) +
    theme_minimal() +   scale_x_continuous(breaks = c(1, 2, 3, 4, 5), labels = c("Almost never", "Infrequently", "Half of the time", "Very common", "All of the time"))  # Relabel x ticks
  
  p5<-ggplot(DF, aes(x = vote2024post, fill = factor(d_state_treatment))) +
    geom_bar(position = position_dodge(width = 0.8), width = 0.8, aes(y = ..prop..)) +
    labs(title = "2024 vote intentions",
         x = "Trust level reported after viewing video",
         y = "Proportion within Condition",
         fill="Condition") +
    scale_fill_manual(values = c("1" = "black", "0" = "grey"), labels = c("1" = "Treated", "0" = "Control")) +
    theme_minimal() +   scale_x_continuous(breaks = c(1, 2, 3, 4, 5), 
                                           labels = c("Definitely will note vote", "Probably will note vote", "May or may not vote", 
                                                      "Probably will vote", "Definitely will vote"))  # Relabel x ticks
  election_preferences_plots <- ggarrange(p1, p2, p3, p4, p5, nrow=5, ncol=1, heights=2, widths=2.2, common.legend = TRUE)
  election_preferences_plots<-annotate_figure(election_preferences_plots, top = text_grob(paste0("Differences between conditions in ",state), 
                                        color = "black", face = "bold", size = 14))
  ggsave(paste0("Mac and Jen Results/Plots/outcome_distributions_",state,".pdf"), election_preferences_plots, height=10, width=8, units="in")
}


#Repeat for pooled sample
DF <- fread("Survey Data/National Recoded.csv")
CO <- fread("Survey Data/Colorado Recoded.csv")
GA <- fread("Survey Data/Georgia Recoded.csv")
LA <- fread("Survey Data/LA Recoded.csv")
TX <- fread("Survey Data/Texas Recoded.csv")

DF$bot <- 0
DF$n<-1:nrow(DF)
#assign sample names
DF$sample<-"USA"
CO$sample<-"CO"
GA$sample<-"GA"
LA$sample <- "LA"
TX$sample <-"TX"

#Remove bots
DF<-rbindlist(list(DF, CO, GA, LA, TX), fill=T)
rm(CO, GA, LA, TX)
DF<-DF[bot==0]

x<-DF |> group_by(d_treatment, ownstatepost) |>
  ## add percentage label with `scales::percent()`
  dplyr::mutate(perc = scales::percent(n / sum(n), accuracy = .1, trim = FALSE))
#create barcharts for each outcome with treatment and control next to eachother and differences noted

p1<-ggplot(DF, aes(x = ownstatepost, fill = factor(d_treatment))) +
  geom_bar(position = position_dodge(width = 0.8), width = 0.8, aes(y = ..prop..)) +
  #  geom_text(aes(label="text"), y=0.3, position_dodge(width=0.8)) +
  labs(title = "Trust in own state",
       x = "Trust level reported after viewing video",
       y = "Proportion within Condition",
       fill="Condition") +
  scale_fill_manual(values = c("1" = "black", "0" = "grey"), labels = c("1" = "Treated", "0" = "Control")) +
  theme_minimal() +   scale_x_continuous(breaks = c(1, 2, 3, 4), labels = c("Distrust A Lot", "Distrust Some", "Trust Some", "Truse A Lot"))  # Relabel x ticks

p2<-ggplot(DF, aes(x = otherstatepost, fill = factor(d_treatment))) +
  geom_bar(position = position_dodge(width = 0.8), width = 0.8, aes(y = ..prop..)) +
  labs(title = "Trust in other states",
       x = "Trust level reported after viewing video",
       y = "Proportion within Condition",
       fill="Condition") +
  scale_fill_manual(values = c("1" = "black", "0" = "grey"), labels = c("1" = "Treated", "0" = "Control")) +
  theme_minimal() +   scale_x_continuous(breaks = c(1, 2, 3, 4), labels = c("Distrust A Lot", "Distrust Some", "Trust Some", "Truse A Lot"))  # Relabel x ticks

p3<-ggplot(DF, aes(x = votefraudpost, fill = factor(d_treatment))) +
  geom_bar(position = position_dodge(width = 0.8), width = 0.8, aes(y = ..prop..)) +
  labs(title = "Belief in fraud by voters",
       x = "Trust level reported after viewing video",
       y = "Proportion within Condition",
       fill="Condition") +
  scale_fill_manual(values = c("1" = "black", "0" = "grey"), labels = c("1" = "Treated", "0" = "Control")) +
  theme_minimal() +   scale_x_continuous(breaks = c(1, 2, 3, 4, 5), labels = c("Almost never", "Infrequently", "Half of the time", "Very common", "All of the time"))  # Relabel x ticks

p4<-ggplot(DF, aes(x = officialfraudpost, fill = factor(d_treatment))) +
  geom_bar(position = position_dodge(width = 0.8), width = 0.8, aes(y = ..prop..)) +
  labs(title = "Belief in fraud by officials",
       x = "Trust level reported after viewing video",
       y = "Proportion within Condition",
       fill="Condition") +
  scale_fill_manual(values = c("1" = "black", "0" = "grey"), labels = c("1" = "Treated", "0" = "Control")) +
  theme_minimal() +   scale_x_continuous(breaks = c(1, 2, 3, 4, 5), labels = c("Almost never", "Infrequently", "Half of the time", "Very common", "All of the time"))  # Relabel x ticks

p5<-ggplot(DF, aes(x = vote2024post, fill = factor(d_treatment))) +
  geom_bar(position = position_dodge(width = 0.8), width = 0.8, aes(y = ..prop..)) +
  labs(title = "2024 vote intentions",
       x = "Trust level reported after viewing video",
       y = "Proportion within Condition",
       fill="Condition") +
  scale_fill_manual(values = c("1" = "black", "0" = "grey"), labels = c("1" = "Treated", "0" = "Control")) +
  theme_minimal() +   scale_x_continuous(breaks = c(1, 2, 3, 4, 5), 
                                         labels = c("Definitely will note vote", "Probably will note vote", "May or may not vote", 
                                                    "Probably will vote", "Definitely will vote"))  # Relabel x ticks
election_preferences_plots <- ggarrange(p1, p2, p3, p4, p5, nrow=5, ncol=1, heights=2, widths=2.2, common.legend = TRUE)
election_preferences_plots<-annotate_figure(election_preferences_plots, top = text_grob("Differences between conditions in pooled sample", 
                                                                                        color = "black", face = "bold", size = 14))
ggsave(paste0("Mac and Jen Results/Plots/outcome_distributions_pooled.pdf"), election_preferences_plots, height=10, width=8, units="in")
